package cn.openea.perpartner.link.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : ysk<ysk@eventslack.com>
 * @description : 通用线程变量
 */
public class CommonContextHolder {

    private static final ThreadLocal<Map<String,Object>> CONTEXT_HOLDER = new ThreadLocal<>();


    public static void addValue(String key,Object value){
        if(CONTEXT_HOLDER.get() == null){
            CONTEXT_HOLDER.set(new HashMap<>());
        }
        CONTEXT_HOLDER.get().put(key,value);
    }

    public static Map<String,Object> getValue(){
        if(CONTEXT_HOLDER.get() == null){
            CONTEXT_HOLDER.set(new HashMap<>());
        }
        return CONTEXT_HOLDER.get();
    }

    public static Object getObject(String key) {
        return CONTEXT_HOLDER.get() == null ? null : CONTEXT_HOLDER.get().get(key);
    }

    public static void remove(){
        CONTEXT_HOLDER.remove();
    }
}
